package com.example.fragmentTab;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.viewpageindicator.R;

import butterknife.Bind;
import butterknife.ButterKnife;

public class TabFragmentActivity extends FragmentActivity implements View.OnClickListener {

    private LinearLayout mWeixinBar;
    private LinearLayout mFrdBar;
    private LinearLayout mAddressBar;
    private LinearLayout mSettingBar;

    @Bind(R.id.bar_weixin_img)
    public ImageButton mWeixinImg;
    @Bind(R.id.bar_frd_img)
    public ImageButton mFrdImg;
    @Bind(R.id.bar_address_img)
    public ImageButton mAddressImg;
    @Bind(R.id.bar_setting_img)
    public ImageButton mSettingImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_fragment);
        ButterKnife.bind(this);

        initView();

        setFragment(0);
    }

    private void initView() {
        mWeixinBar = (LinearLayout) findViewById(R.id.bar_weixin_view);
        mFrdBar = (LinearLayout) findViewById(R.id.bar_frd_view);
        mAddressBar = (LinearLayout) findViewById(R.id.bar_address_view);
        mSettingBar = (LinearLayout) findViewById(R.id.bar_setting_view);

        mWeixinBar.setOnClickListener(this);
        mFrdBar.setOnClickListener(this);
        mAddressBar.setOnClickListener(this);
        mSettingBar.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        resetImg();
        int id = v.getId();

        switch (id) {
            case R.id.bar_weixin_view:
                mWeixinImg.setImageResource(R.drawable.tab_weixin_pressed);
                setFragment(0);
                break;
            case R.id.bar_frd_view:
                mFrdImg.setImageResource(R.drawable.tab_find_frd_pressed);
                setFragment(1);
                break;
            case R.id.bar_address_view:
                mAddressImg.setImageResource(R.drawable.tab_address_pressed);
                setFragment(2);
                break;
            case R.id.bar_setting_view:
                mSettingImg.setImageResource(R.drawable.tab_settings_pressed);
                setFragment(3);
                break;
            default:
                break;
        }
    }


    public void setFragment(int id){
        String tag = null;
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        Fragment fragment = null;

        switch (id) {
            case 0:
                tag = WeixinFragment.class.getSimpleName();
                fragment = fm.findFragmentByTag(tag);
                if(fragment == null){
                    fragment = new WeixinFragment();
                }
                break;
            case 1:
                tag = FrdFragment.class.getSimpleName();
                fragment = fm.findFragmentByTag(tag);
                if(fragment == null){
                    fragment = new FrdFragment();
                }
                break;
            case 2:
                tag = AddressFragment.class.getSimpleName();
                fragment = fm.findFragmentByTag(tag);
                if(fragment == null){
                    fragment = new AddressFragment();
                }
                break;
            case 3:
                tag = SettingFragment.class.getSimpleName();
                fragment = fm.findFragmentByTag(tag);
                if(fragment == null){
                    fragment = new SettingFragment();
                }
                break;
            default:
                break;
        }

        transaction.replace(R.id.tab_fragment,fragment,tag).commit();
    }

    private void resetImg() {
        mWeixinImg.setImageResource(R.drawable.tab_weixin_normal);
        mFrdImg.setImageResource(R.drawable.tab_find_frd_normal);
        mAddressImg.setImageResource(R.drawable.tab_address_normal);
        mSettingImg.setImageResource(R.drawable.tab_settings_normal);
    }


}
